package org.mozhu.mboot.admin.showcase.crud.web.controller;

import org.mozhu.mboot.admin.showcase.crud.entity.Sample;
import org.mozhu.mboot.admin.showcase.crud.service.SampleService;
import org.mozhu.mboot.core.web.bind.annotation.ControllerDefaults;
import org.mozhu.mboot.core.web.controller.BaseCRUDController;
import org.mozhu.mboot.admin.system.account.entity.Gender;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 示例 - 控制层
 *
 * @author ZhangPei
 */
@Controller
@RequestMapping("/showcase/crud/sample")
@ControllerDefaults(resourceIdentity = "showcase:sample")
public class SampleController extends BaseCRUDController<Sample, Long> {

    protected SampleService getBaseService() {
        return (SampleService) baseService;
    }

    @Override
    protected void setCommonData(Model model) {
        super.setCommonData(model);
        model.addAttribute("genders", Gender.values());
    }

    @RequestMapping(value = "/validate")
    @ResponseBody
    public Object validate(@RequestParam(value = "field") String field, @RequestParam(value = "fieldValue") String fieldValue,
                           @RequestParam(value = "id", required = false) String id) {
        if ("email".equals(field)) {
            Sample user = getBaseService().findByEmail(fieldValue);
            return user == null || (user.getId().toString().equals(id));
        }
        return null;
    }
}